'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "";
            if($state.includes('**.information.update')){
                title="编辑资讯模块信息";
                var moduleNo = $state.params.id;
                activate(moduleNo);
                $scope.isDisabled = false;
            }else if($state.includes('**.information.create')){
                title="添加资讯模块";
                setTimeout(function(){
                    !$rootScope.$$phase && $scope.$apply();
                },300);
            }
            $scope.title = $rootScope.title = title;
            $scope.loading = true;

            //初始化验证
            $scope.submit= function(){
                $scope.loading = true;
                var sortNum =$('#sortNum').val();
                if(!$('#moduleName').val()){
                    $scope.alertModal({'content':'模块名称不能为空'});
                    return;
                }

                if(isNaN(sortNum)||sortNum<=0||!(/^\d+$/.test(sortNum))){
                    $scope.alertModal({'content':'排序号格式不正确'});
                    return ;
                }

                if(!$('#sortNum').val()){
                    $scope.alertModal({'content':'排序号不能为空'});
                    return;
                }
                if($('#sortNum').val().length>2){
                    $scope.alertModal({'content':'排序号长度不能超过2位'});
                    return;
                }
                /*if($('#sortNum').val()<=($scope.pageInfo.records.length)){
                    $scope.alertModal({'content':'排序号重复，请重新输入'});
                    return;
                }*/
                if(!$('#enable').val()){
                    $scope.alertModal({'content':'是否可用不能为空'});
                    return;
                }

                if(!$('#introduction').val()){
                    $scope.alertModal({'content':'详情不能为空'});
                    return;
                }else{
                    if(introduction.length>150){
                        $scope.alertModal({"content":"内容不能超过150个字"});
                        return;
                    }
                }
                saveData();
            };

            function saveData(){
                var m = $scope.record;
                console.log(m);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.record.moduleNo ? $scope.app.project_name+'/information/read/update' : $scope.app.project_name+'/information/read/add',
                        data: m
                    }).then(callback);
                }
                function callback(result){
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            $state.go('main.plat.information.list');
                        },2000);
                    }else{
                        $scope.alertModal({"content":"操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;
                }
            }

            // 初始化页面
            function activate(moduleNo) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/information/read/detail',
                    data: {'id': moduleNo}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

        }]);